package com.kuiandjie.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.kuiandjie.entity.Employee;
import com.kuiandjie.service.ProjectService;

public class CreateProServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		HttpSession session = request.getSession();
		
		Employee emp = (Employee) session.getAttribute("employee");
		String name = emp.getName();
		int id = emp.getId();
		
		if("tom".equals(name)) {
			String proName = request.getParameter("proname");
			String leader = request.getParameter("leader");
			String member = request.getParameter("member");
			String desc = request.getParameter("desc");
			
			if(proName != null && leader != null && member != null && desc != null) {
				createProject(id, proName, leader, member, desc);
				response.sendRedirect("main.jspx");
			} else {
				response.sendError(404, "页面不存在");
			}
		} else {
			response.sendError(404, "页面不存在");
		}
	}

	private void createProject(int id, String proName, String leader, String member,
			String desc) {
		int leaderId = Integer.parseInt(leader);
		String[] memberIds = member.split(",");
		ProjectService ps = new ProjectService();
		ps.createNewProject(id, proName, leaderId, memberIds, desc);
	}

}
